/*
  @description: 测试类
 * @author: L.Wen
 * @create: 2018-09-29 14:21
 */

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class TestXiciIP {
    private static WebDriver driver;
    private WebElement webElement = null;
    private WebElement webElement1 = null;

    static {
        //告诉系统chromedriver.exe的位置
        System.setProperty("webdriver.chrome.driver", "C:\\Programs\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
    }

    /**
     * 打开西刺网站
     */
    @Test()
    public void openXiCi() {
        System.out.println("Test openXiCi! \n");
        driver.get("http://www.xicidaili.com/nt/");
        driver.manage().window().maximize();
    }

    @Test(priority = 10)
    public void searchIP() {
        System.out.println("Test searchIP! \n");
        webElement = driver.findElement(By.xpath("//table[@id=\"ip_list\"]"));
        List<WebElement> webElements = webElement.findElements(By.xpath("//tr[@class=\"odd\"]"));
        System.out.println("size: " + webElements.size());
        if (webElements.size() > 0) {
            List<String> ipList = new ArrayList<>();
            for (int i = 0; i < webElements.size(); i++) {
                String ip = webElements.get(i).findElement(By.xpath(".//td[2]")).getText();
                System.out.println("ip: " + ip);
                ipList.add(ip);
            }
            System.out.println("ipList: " + ipList);
        }

    }
}

